<?php
function array_get ( array $arr, $key, $def_val = null )
{
	return array_key_exists ( $key, $arr ) ? $arr [ $key ] : $def_val;
}

function smart_addslashes ( $str )
{
        if ( ( strstr ( $str, "\\'" ) === false ) && ( strstr ( $str, "\\\"" ) === false ) )
                return addslashes ( $str );

        return $str;
}

class LiWEUtils 
{
	const SAFE_UNSET = 0;
	const SAFE_NUMBER = 1;
	const SAFE_STRING = 2;
	const SAFE_DB_STRING = 3;
	const SAFE_MONEY = 4;
	const SAFE_BOOL = 5;
	const SAFE_DATE = 6;
	const SAFE_DB_BOOL = 7;
	const SAFE_HTML = 8;
	const SAFE_DB_HTML = 9;
	const SAFE_EMAIL = 10;
	
	public function __construct ()
	{
		$this->_mode_names = array (
			"NUMBER" => self::SAFE_NUMBER,
			"STRING" => self::SAFE_STRING,
			"DB_STRING" => self::SAFE_DB_STRING,
			"MONEY" => self::SAFE_MONEY,
			"BOOL" => self::SAFE_BOOL,
			"DATE" => self::SAFE_DATE,
			"DB_BOOL" => self::SAFE_DB_BOOL,
			"HTML" => self::SAFE_HTML,
			"DB_HTML" => self::SAFE_DB_HTML,
			"SAFE_EMAIL" => self::SAFE_EMAIL
		);
	}

	public function safe_get_mode ( $name )
	{
		return array_get ( $this->_mode_names, strtoupper ( $name ), 0 );
	}
	
	private function _map_entities ( $txt )
	{
		$entities = array (
			array ( "&#8217;", "'" ),
			array ( "&#224;", "&agrave;" ),
			array ( "&#232;", "&egrave;" ),
			array ( "&#233;", "&eacute;" ),
			array ( "&#242;", "&ograve;" ),
			array ( "&#249;", "&ugrave;" ),
			array ( "&#8220;", '"' ),
			array ( "&#8221;", '"' ),
			array ( "&#8211;", "-" ),
			array ( "%u2013", "-" ),
			array ( "%u2019", "'" ),
			array ( "%u201C", '"' ),
			array ( "%u201D", '"' )
		);
		
		foreach ( $entities as $value) {
			$txt = str_replace($value [ 0 ], $value [ 1 ], $txt );
		}

		return $txt;
	}

	public function get ( $name, $mode = "PGS", $safe = 0, $def_val = '', $def_user_val = 0xDEADBEEF )
	{
		$val = $def_val;
		
		foreach ( str_split ( $mode ) as $m )
		{
			print "MODE: $m - name: $name<br />";
			switch ( $m )
			{
				case "U":
					$val = $def_user_val;
					break;
				
				case "P":
					$val = array_get ( $_POST, $name, 0xDEADBEEF );
					break;
					
				case "G":
					$val = array_get ( $_GET, $name, 0xDEADBEEF );
					break;
				case "S":
					print "<h1>LiWEUTILS::get() missing 'S' MODE";
					$val = 0xDEADBEEF;
			}
			
			if ( $val != 0xDEADBEEF ) break;
		}
		
		if ( ! $safe ) return $val;
		
		return $this->_do_safe ( $val, $safe );
	}
	
	private function _do_safe ( $val, $safe )
	{
		$val = trim ( $val );
		$res = $this->_map_entities ( $val );
		
		switch ( $safe )
		{
			case self::SAFE_NUMBER:
				if ( is_numeric ( $res ) == false ) return 0;
				return $res + 0;

			case SAFE_MODE_DB_STRING:
                        	$v = smart_addslashes ( $v );
                        	break;

                	case SAFE_MODE_UNSET:
                        	break;

                case SAFE_MODE_MONEY:
                        $v = explode ( ",", str_replace ( ".", "", $v ) );
                        $v = sprintf ( "%d%02.2d", $v [ 0 ], $v [ 1 ] );
                        break;

                case SAFE_MODE_BOOL:
                        if ( $v == 'false' ) $v = 0;
                        if ( $v ) $v = true;
                        else      $v = false;
                        break;

                case SAFE_MODE_DB_BOOL:
                        if ( $v ) $v = "1";
                        else      $v = "0";
                        break;

                case SAFE_MODE_DATE:
                        preg_match ( '/([0-9]{4}).([0-9]{1,2}).([0-9]{1,2})/', $v, $arr );
                        $v = sprintf ( "%04.4d-%02.2d-%02.2d", $arr [ 1 ], $arr [ 2 ], $arr [ 3 ] );
                        if ( $v == '0000-00-00' ) $v = false;
                        break;

                case SAFE_MODE_HTML:
                        $v = util_html_text ( $v );
                        break;

                case SAFE_MODE_DB_HTML:
                        $v = str_replace ( "'", "\\'", util_html_text ( $v ) );
                        break;

                case SAFE_MODE_STRING:
                default:
                        if ( is_string ( $v ) ) $v = stripslashes ( $v );

		}
		
		return $res;
	}

/*
		val = str ( val ).strip ()
		res = _map_entities ( val )

		if safe == self.SAFE_NUMBER:
			try:
				res = int ( val, 10 )
			except:
				res = 0

		elif safe == self.SAFE_DB_STRING:
			res = res.replace ( "'", "''" )

		elif safe == self.SAFE_MONEY:
			m = money.Money ( val )
			res = m.to_long_int ()

		elif safe == self.SAFE_BOOL:
			if val in ( 'false', '0', 0 ): 
				val = 0

			if val: res = True
			else: res = False

		elif safe == self.SAFE_DB_BOOL:
			if val in ( 'false', '0', 0 ): 
				val = 0

			if val: res = 1
			else: res = 0

		elif safe == self.SAFE_DATE:
			# FIXME: da fare
			res = val.replace ( "'", "''" )

		elif safe == self.SAFE_HTML:
			# FIXME: da fare
			res = _map_entities ( val )

		elif safe == self.SAFE_DB_HTML:
			# FIXME: da fare
			res = _map_entities ( val )
			res = res.replace ( "'", "''" )

		elif safe == self.SAFE_EMAIL:
			# FIXME: da finire
			res = val.replace ( "'", "" )
			

		return res
	 * 
	 */
	
	
	
	
	
}
?>